package com.android.kotlincoroutinemvvmdemo.view

import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.android.kotlincoroutinemvvmdemo.R

class ArcProgressView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context,attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private var mStartAngle = -90F


    private var startAngle : Float = mStartAngle
        set(value) {
            field = value
            invalidate()
        }

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_10)
            color = context.resources.getColor(R.color.color_03a9f4)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL_AND_STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_10)
            color = context.resources.getColor(R.color.color_673ab7)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            canvas.translate(mWidth.toFloat() / 2,mHeight.toFloat() / 2)
            drawCircle(
                0F,
                0F,
                mWidth.toFloat() * 5 / 12,
                mPaint0
            )
        }
    }

    override fun dispatchDraw(canvas: Canvas?) {
        super.dispatchDraw(canvas)
        canvas?.apply {
            drawArc(
                -mWidth.toFloat() * 5 / 12,
                -mWidth.toFloat() * 5 / 12,
                mWidth.toFloat() * 5 / 12 ,
                mWidth.toFloat() * 5 / 12,
                startAngle,
                45F,
                false,
                mPaint1
            )
        }
    }

    fun startSwipe() {
        val objectAnimator = ObjectAnimator.ofFloat(this, "startAngle", startAngle, 270F)
        objectAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}
            override fun onAnimationCancel(animation: Animator) {}
            override fun onAnimationRepeat(animation: Animator) {}
            override fun onAnimationEnd(animation: Animator) {
                //Log.d(TAG,"---onAnimationEnd---")
                //mStartAngle += floatArray[i]
            }
        })
        objectAnimator.duration = 3000
        //使用差值器避免中间会出现的停顿
        objectAnimator.interpolator = LinearInterpolator()
        objectAnimator.repeatMode = ValueAnimator.RESTART
        objectAnimator.repeatCount = ValueAnimator.INFINITE
        objectAnimator.start()
    }


    companion object {
        const val TAG = "AnimationView"
    }
}